package cn.impl;

import cn.basic.HibBaseDAOImpl;
import cn.basic.iHibBaseDAO;
import cn.common.properties.EquipmentType;
import cn.dao.EquipmentLogDAO;
import cn.model.TEquipmentSysLog;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component("logdao")
public class EquipmentLogDAOImpl implements EquipmentLogDAO {
    //连接数据库操作层接口,实现业务与数据的交换
    private iHibBaseDAO bdao= null;

    public EquipmentLogDAOImpl(){
        bdao = new HibBaseDAOImpl();
    }

    /**
     * 根据条件获取系统管理日志的列表
     *
     * @param wherecondition 组合查询条件字符串,如："userRole = '超级管理员' and userid = 'zhangjs'"
     * @param currentPage    按分页查询的当前页
     * @param pageSize       按分页查询的每页数量
     * @return List
     */
    @Override
    public List<TEquipmentSysLog> getEquipmentLogList(String wherecondition, int currentPage, int pageSize) {
        String hql = "from TEquipmentSysLog";
        if (wherecondition == null && wherecondition.equals("")){
            hql += "order by createdate desc";
        }
        if (wherecondition != null && !wherecondition.equals("")){
            hql += wherecondition;
        }
        List<TEquipmentSysLog> list = bdao.selectByPage(hql, currentPage, pageSize);
        return list;
    }

    /**
     * 根据条件获取符合条件的系统管理日志的数量
     *
     * @param wherecondition 如："userRole = '超级管理员' and userid = 'zhangjs'"
     * @return
     */
    @Override
    public int getEquipmentLogAmount(String wherecondition) {
        String hql = "select count(*) from TEquipmentSysLog ";
        if(wherecondition!=null && !wherecondition.equals("")){
            hql += wherecondition;
        }
        return bdao.selectValue(hql);
    }

    /**
     * 获取系统操作的类型
     *
     * @return
     */
    @Override
    public List<String> getOperTypes() {
        List<String> list = new ArrayList<String>();
        for(int i = 0; i< EquipmentType.OPERTYPES.length; i++){
            list.add(EquipmentType.OPERTYPES[i]);
        }

        return list;
    }

    /**
     * 根据主键id删除日志
     *
     * @param eqsysid
     * @return
     */
    @Override
    public boolean deleteLogById(Integer eqsysid) {
        return bdao.delete(TEquipmentSysLog.class, eqsysid);
    }

    /**
     * 添加一条操作日志
     *
     * @param log
     * @return
     */
    @Override
    public Integer addLog(TEquipmentSysLog log) {
        return (Integer)bdao.insert(log);
    }

    /**
     * 返回对应主键id的日志记录
     *
     * @param eqsysid
     * @return
     */
    @Override
    public TEquipmentSysLog getLogById(Integer eqsysid) {
        return (TEquipmentSysLog)bdao.findById(TEquipmentSysLog.class, eqsysid);
    }

    /**
     * 清理所有日志
     *
     * @return
     */
    @Override
    public boolean deleteAllLog() {
        String hql = "Delete from T_EquipmentSysLog ";
        if (bdao.delete(hql)) {
            return true;
        }else {
            return false;
        }
    }
}
